package com.moo2u2.android.oaktononthego.util;

import java.util.Calendar;
import java.util.Locale;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.moo2u2.android.oaktononthego.R;

/**
 * Utilities class for random methods used by all.
 * 
 * @author Moo2u2
 */
public class Util {
    /**
     * Date format for displaying dates.
     */
    public static final String DATE_FORMAT = "dd/MM/yyyy";

    /**
     * Key for shared preferences.
     */
    private static final String SHARED_PREFS = "oaktononthego"
            .toUpperCase(Locale.ENGLISH) + "_PREFS";

    /**
     * An intent name for receiving colleague updates.
     */
    public static final String UPDATE_COLLEAGUES_INTENT = getPackageName()
            + ".UPDATE_UI";

    /**
     * Figures out the end of the week for a given day.
     * 
     * @param today
     *            Day for which to find end of the week.
     * @return End of the week.
     */
    public static Calendar findEndOfWeek(Calendar today) {
        int weekday = today.get(Calendar.DAY_OF_WEEK);
        int days = Calendar.SUNDAY - weekday;
        if (days < 0)
            today.add(Calendar.DAY_OF_YEAR, days + 7);
        return today;
    }

    /**
     * Calculates the distance between two locations
     * 
     * @param pointA
     *            Location of point A
     * @param pointB
     *            GeoPoint of point B
     * @return Distance between the two points in metres.
     */
    public static float calculateDistance(Location pointA, GeoPoint pointB) {
        float[] results = new float[3];
        Location.distanceBetween(pointA.getLatitude(), pointA.getLongitude(),
                pointB.getLatitudeE6() / 1E6, pointB.getLongitudeE6() / 1E6,
                results);
        return results[0];
    }

    /**
     * Returns the package name of this class.
     */
    private static String getPackageName() {
        return Util.class.getPackage().getName();
    }

    /**
     * Helper method to get a SharedPreferences instance.
     */
    public static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(SHARED_PREFS, 0);
    }

    /**
     * Display a notification containing the given string.
     */
    public static void generateNotification(Context context, String message) {
        int icon = R.drawable.status_icon;
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, message, when);
        notification.setLatestEventInfo(context, "C2DM Example", message,
                PendingIntent.getActivity(context, 0, null,
                        PendingIntent.FLAG_CANCEL_CURRENT));
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        SharedPreferences settings = Util.getSharedPreferences(context);
        int notificatonID = settings.getInt("notificationID", 0);

        NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(notificatonID, notification);

        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("notificationID", ++notificatonID % 32);
        editor.commit();
    }
}
